package com.smarthospital.service.impl;

import com.google.common.collect.Lists;
import com.smarthospital.contants.SHDictionaryEnum;
import com.smarthospital.dao.daoService.SHDictionaryDaoService;
import com.smarthospital.entity.pojo.Doctor;
import com.smarthospital.entity.pojo.SHDictionary;
import com.smarthospital.entity.pojo.User;
import com.smarthospital.service.SHDictionaryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * create by sym  2021/5/29 14:18.
 */
@Slf4j
@Service
public class SHDictionaryServiceImpl implements SHDictionaryService {

    private static String[] doctorDictionKeys = {
            SHDictionaryEnum.SH_DOCTOR_LEVEL.getDictionary(),
            SHDictionaryEnum.SH_DOCTOR_MAJOR.getDictionary(),
            SHDictionaryEnum.SH_DOCTOR_TITLE.getDictionary(),
            SHDictionaryEnum.SH_DOCTOR_HOSPITAL.getDictionary()
    };





    @Autowired
    private SHDictionaryDaoService dictionaryDaoService;

    @Override
    public void resolveDoctorDictionFields(List<Doctor> list) {
        List<SHDictionary> shDictionaries = dictionaryDaoService.queryIndictionaryCode(Lists.newArrayList(doctorDictionKeys));
        Map<String, List<SHDictionary>> map = shDictionaries.stream().collect(Collectors.groupingBy(SHDictionary::getDictionary));
        list.stream().forEach(d->{
            //处理DoctorLevelStr,字典没有对应的话 就不处理
            List<SHDictionary> shDictionaries1 = map.get(SHDictionaryEnum.SH_DOCTOR_LEVEL.getDictionary());
            if (!CollectionUtils.isEmpty(shDictionaries1)) {
                shDictionaries1.stream().filter(dic -> dic.getKey().equals(d.getDoctorLevel() + "")).findFirst().ifPresent(shDictionary -> d.setDoctorLevelStr(shDictionary.getValue()));
            }
            //mayor
            List<SHDictionary> shDictionaries2 = map.get(SHDictionaryEnum.SH_DOCTOR_MAJOR.getDictionary());
            if (!CollectionUtils.isEmpty(shDictionaries2)) {
                shDictionaries2.stream().filter(dic -> dic.getKey().equals(d.getMajor() + "")).findFirst().ifPresent(shDictionary -> d.setMajor(shDictionary.getValue()));
            }
            //hospital
            List<SHDictionary> shDictionaries3 = map.get(SHDictionaryEnum.SH_DOCTOR_HOSPITAL.getDictionary());
            if (!CollectionUtils.isEmpty(shDictionaries3)) {
                shDictionaries3.stream().filter(dic -> dic.getKey().equals(d.getHospital() + "")).findFirst().ifPresent(shDictionary -> d.setHospital(shDictionary.getValue()));
            }
            //SH_DOCTOR_TITLE
            List<SHDictionary> shDictionaries4 = map.get(SHDictionaryEnum.SH_DOCTOR_TITLE.getDictionary());
            if (!CollectionUtils.isEmpty(shDictionaries4)) {
                shDictionaries4.stream().filter(dic -> dic.getKey().equals(d.getTitle() + "")).findFirst().ifPresent(shDictionary -> d.setTitle(shDictionary.getValue()));
            }

        });

    }

    private static String[] userDictionKeys = {
            SHDictionaryEnum.SYSTEM_SEX.getDictionary(),
            SHDictionaryEnum.SYSTEM_NATIONALITY.getDictionary(),
            SHDictionaryEnum.SH_PATIENT_TYPE.getDictionary(),
            SHDictionaryEnum.SYSTEM_MARITAL.getDictionary(),
            SHDictionaryEnum.SH_DOCUMENT_TYPE.getDictionary(),
            SHDictionaryEnum.SYSTEM_OCCUPATION.getDictionary(),
            SHDictionaryEnum.SYSTEM_PROVINCE.getDictionary(),
            SHDictionaryEnum.SYSTEM_CITY.getDictionary(),
            SHDictionaryEnum.SYSTEM_NATIONALITY.getDictionary()
    };
    @Override
    public User resolveUserDictionFields(User user) {
        ArrayList<String> keys = Lists.newArrayList(userDictionKeys);
        List<SHDictionary> shDictionaries = dictionaryDaoService.queryIndictionaryCode(keys);
        Map<String, List<SHDictionary>> map = shDictionaries.stream().collect(Collectors.groupingBy(SHDictionary::getDictionary));
        //性别
        List<SHDictionary> shDictionaries1 = map.get(SHDictionaryEnum.SYSTEM_SEX.getDictionary());
        if (!CollectionUtils.isEmpty(shDictionaries1)) {
            shDictionaries1.stream().filter(dic -> dic.getKey().equals(user.getGender() + "")).findFirst().ifPresent(shDictionary -> user.setGender(shDictionary.getValue()));
        }
        //民族
        List<SHDictionary> shDictionaries2 = map.get(SHDictionaryEnum.SYSTEM_NATIONALITY.getDictionary());
        if (!CollectionUtils.isEmpty(shDictionaries2)) {
            shDictionaries2.stream().filter(dic -> dic.getKey().equals(user.getNation() + "")).findFirst().ifPresent(shDictionary -> user.setNation(shDictionary.getValue()));
        }
        //婚姻
        List<SHDictionary> shDictionaries3 = map.get(SHDictionaryEnum.SYSTEM_MARITAL.getDictionary());
        if (!CollectionUtils.isEmpty(shDictionaries3)) {
            shDictionaries3.stream().filter(dic -> dic.getKey().equals(user.getMarryState() + "")).findFirst().ifPresent(shDictionary -> user.setMarryStateStr(shDictionary.getValue()));
        }
        //病人类型 一档 二档...
        List<SHDictionary> shDictionaries4 = map.get(SHDictionaryEnum.SH_PATIENT_TYPE.getDictionary());
        if (!CollectionUtils.isEmpty(shDictionaries4)) {
            shDictionaries4.stream().filter(dic -> dic.getKey().equals(user.getPatientType() + "")).findFirst().ifPresent(shDictionary -> user.setPatientType(shDictionary.getValue()));
        }
        //证件类型
        List<SHDictionary> shDictionaries5 = map.get(SHDictionaryEnum.SH_DOCUMENT_TYPE.getDictionary());
        if (!CollectionUtils.isEmpty(shDictionaries5)) {
            shDictionaries5.stream().filter(dic -> dic.getKey().equals(user.getCertificateType() + "")).findFirst().ifPresent(shDictionary -> user.setCertificateTypeStr(shDictionary.getValue()));
        }
        //国籍
        List<SHDictionary> shDictionaries6 = map.get(SHDictionaryEnum.SYSTEM_NATIONALITY.getDictionary());
        if (!CollectionUtils.isEmpty(shDictionaries6)) {
            shDictionaries6.stream().filter(dic -> dic.getKey().equals(user.getCountry() + "")).findFirst().ifPresent(shDictionary -> user.setCountry(shDictionary.getValue()));
        }
        //职业
        List<SHDictionary> shDictionaries7 = map.get(SHDictionaryEnum.SYSTEM_OCCUPATION.getDictionary());
        if (!CollectionUtils.isEmpty(shDictionaries7)) {
            shDictionaries7.stream().filter(dic -> dic.getKey().equals(user.getCareer() + "")).findFirst().ifPresent(shDictionary -> user.setCareer(shDictionary.getValue()));
        }
        //省份
        List<SHDictionary> shDictionaries8 = map.get(SHDictionaryEnum.SYSTEM_PROVINCE.getDictionary());
        if (!CollectionUtils.isEmpty(shDictionaries8)) {
            shDictionaries8.stream().filter(dic -> dic.getKey().equals(user.getProvince() + "")).findFirst().ifPresent(shDictionary -> user.setProvince(shDictionary.getValue()));
        }
        //city
        List<SHDictionary> shDictionaries9 = map.get(SHDictionaryEnum.SYSTEM_CITY.getDictionary());
        if (!CollectionUtils.isEmpty(shDictionaries9)) {
            shDictionaries9.stream().filter(dic -> dic.getKey().equals(user.getCity() + "")).findFirst().ifPresent(shDictionary -> user.setCity(shDictionary.getValue()));
        }
        return user;
    }
}
